package com.lzy.chat.d_controller;

import com.lzy.chat.a_entity.AddFriend;
import com.lzy.chat.a_entity.Friend;
import com.lzy.chat.a_entity.Message;
import com.lzy.chat.a_entity.User;
import com.lzy.chat.c_service.FriendService;
import com.lzy.chat.c_service.UserService;
import com.lzy.chat.c_service.WebsocketService;
import com.lzy.chat.e_util.JsonResult;
import com.lzy.chat.g_annotation.CurrentUser;
import com.lzy.chat.g_annotation.NeedToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("friend")
public class FriendController extends BaseController {

    private FriendService friendService;
    private UserService userService;

    @Autowired
    public void setFriendService(FriendService friendService) {
        this.friendService = friendService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @NeedToken
    @RequestMapping("accept")
    public JsonResult<Void> addFriend(AddFriend addFriend) {
        friendService.dealAddFriend(addFriend.getId(), 1);
        friendService.addFriend(addFriend.getFromId(), addFriend.getToId());
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("reject")
    public JsonResult<Void> rejectFriend(Integer addFriendId) {
        friendService.dealAddFriend(addFriendId, 2);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("getFriendList")
    public JsonResult<List<User>> getFriendList(@CurrentUser User user) {
        List<Friend> list = friendService.getFriendListByUserId(user.getId());
        List<User> friendList = new ArrayList<>();
        for (Friend f :
                list) {
            Integer friendId = Objects.equals(user.getId(), f.getUserId()) ? f.getFriendId() : f.getUserId();
//            System.out.println(friendId);
            User u = userService.getInfoById(friendId);
            friendList.add(u);
        }
        return new JsonResult<>(ok, friendList);
    }

    @NeedToken
    @RequestMapping("isFriend")
    public JsonResult<Boolean> isFriend(@CurrentUser User user, Integer userId) {
        boolean isFriend = friendService.isFriend(user.getId(), userId);
        return new JsonResult<>(ok, isFriend);
    }

    @NeedToken
    @RequestMapping("deleteFriend")
    public JsonResult<Boolean> deleteFriend(@CurrentUser User user, Integer userId) {
        friendService.deleteFriend(user.getId(), userId);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("requestToAdd")
    public JsonResult<Void> requestToAddFriend(AddFriend addFriend) throws IOException {
        friendService.requestToAddFriend(addFriend);
        Message message = new Message();
        message.setMessageType(5);
        message.setData("1");
        message.setToId(addFriend.getToId());
        WebsocketService.sendToUser(message);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("getUnDealRequest")
    public JsonResult<Map<String, Object>> getUnDealRequest(@CurrentUser User user) {
        List<AddFriend> list = friendService.getUnDealByUid(user.getId());
        List<User> users = new ArrayList<>();
        for (AddFriend a : list) {
            int id = user.getId().equals(a.getToId()) ? a.getFromId() : a.getToId();
            users.add(userService.getInfoById(id));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("friendRequest", list);
        map.put("users", users);
        return new JsonResult<>(ok, map);
    }
}
